<?php


namespace app\api\controller;


use app\common\controller\Api;

class Goods  extends Api
{
    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];

    public function getGoodsList()
    {

        $params =$this->request->param();
        $data=(new \app\common\model\Goods())->goodsList($params);
        $this->success('',$data);
    }

    public function goodsDetail()
    {
        $params =$this->request->get();
        $data=(new \app\common\model\Goods())->where(['status'=>1,'id'=>$params['id']])->find();
        if($data){
            $data['goods_image'] =cdnurl($data['goods_image'],true);
            $data['goods_images']=explode(',',$data['goods_images']);
            $goods_images=[];
            foreach ($data['goods_images'] as $k1=>$data1){
                $goods_images[]=cdnurl($data1,true);
            }
            $data['goods_images']=$goods_images;
            $data['goods_sales'] =$data['goods_sales']+$data['base_sales'];
            unset($data['base_sales']);
        }
        $this->success('',$data);
    }

    public function checkStock()
    {
        $param =$this->request->param('');
        $goods = (new \app\common\model\Goods())->where(['id'=>$param['goods_id'],'status'=>1])->find();
        if(!$goods){
            $this->error('商品已下架',0);
        }
        if($goods['goods_num']<$param['num']){
            $this->error('商品库存不足',$goods['goods_num']);
        }
        $this->success('');
    }
}